#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>

void swap(int v[], int i, int j)
{
	int iTmp = 0;

	assert (NULL != v);

	iTmp = v[i];
	v[i] = v[j];
	v[j] = iTmp;
}

void quicksort(int v[], int n)
{
	int i = 0;
	int last = 0;
	int pivot = 0;

	assert(NULL != v);

	if (n <= 1)
	{
		return;		/* do nothing */
	}

	pivot = rand() % n;
	swap(v, 0, pivot);		/* move pivot to v[0] */
	for (; i < n; i++)
	{
		if (v[i] < v[0])
		{
			swap(v, ++last, i);
		}
	}
	swap(v, 0, last);

	quicksort(v, last);
	quicksort(v + last + 1, n - last - 1);
}

int main(void)
{
	int aiArr[] = {1, 3432, 56, 678, 688, 90785, 56, 32, 234, 78, 23, 24890, 872};
	int iNum = 0;

	srand(time(NULL));
	iNum = (int)(sizeof(aiArr) / sizeof(aiArr[0]));

	quicksort(aiArr, iNum);
	return(0);
}
